/**
 * Description:一些注释<br/>
 * CreateTime:2019-04-18<br/>
 * Creater: yangpengfei@think.cn <br/>
 */
var startPageNumber = 1;//起始页数
var totalPageNumber = 0;//总页数
var tableDataLength = 0;//总数据量
var currentPageNumber = 1;//当前页

layui.use(['form','jquery','table','element','layer'],function(){
    //初始化定义变量
    let $ = layui.jquery;
    let form = layui.form;
    let table = layui.table;
    let element = layui.element;
    let layer = layui.layer;
    //定义表格属性
    let tableOptions = {
        id: 'userInfoItems',
        skin: 'line',
        elem: '#userInfoTable',
        height: 'full-20',
        url: '/userInfo/list', //数据接口
        even:true,
        page: {
            groups:5,
            curr:currentPageNumber,
            limit:10,
            limits:[10],
            layout:['count','prev','page','next','limit','refresh','skip'],
            jump:function(obj,first){
                currentPageNumber = obj.curr;
            }
        }, //开启分页
        loading:true,
        text:"当前暂时没有员工信息，请手动配置！",
        toolbar:"#toolbarDemo",
        defaultToolbar:['filter', 'print', 'exports'],
        cols: [[ //表头
            {type:'numbers'},
            {type: 'checkbox'},
            {field: 'userId', title: '员工编号',width:100,unresize:true},
              {field: 'realName', title: '员工姓名',unresize:true},
              {field: 'userName', title: '用户名',unresize:true},
              {field: 'cardId', title: '身份证号码',unresize:true},
              {field: 'birthDay', title: '生日',unresize:true},
              {field: 'roleName',title:'所属角色',unresize:true}
        ]],
        done:function(res,curr,count){
            tableDataLength = res.data.length;
        }
    };
    const initTable = function (){
          table.init('userInfoFilter',tableOptions);
      }
    const reloadTable = function(isEmpty){
        table.reload('userInfoItems',{
            page:{
                curr:isEmpty ?(currentPageNumber == 1 ? currentPageNumber : currentPageNumber-1):currentPageNumber
            }
        });
    }
    //初始化加载表格
    initTable();
    //监听头工具栏事件
    table.on('toolbar(userInfoFilter)', function(obj){
    	let checkStatus = table.checkStatus(obj.config.id);
	    let data = checkStatus.data;
    	switch(obj.event){
        	case 'add':
        		layer.open({
	  				  title:"新增员工信息"
	  				  ,type: 1
	  			        ,offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
	  			        ,id: 'insertUserInfoPanel' //防止重复弹出
	  			        ,content: $('#userInfoPanel')
	  			        ,btn: ['确认', '取消']
      					,area:['800px','400px']
	  			        ,btnAlign: 'c' //按钮居中
	  			        ,closeBtn:2
	  			        ,shade: 0.5 //不显示遮罩
	  			        ,shadeClose:true
	  			        ,anim:1
	  			        ,resize:false
	  			        ,success: function(layero, index){
	  			    		form.val("dataSourceInfoFilter",{
	  			    			"dbType":"",
	  			    			"dbNameText":"",
	  			    			"passText":"",
	  			    			"dbAccessIpText":"",
	  			    			"dbPortText":""
	  			    		});
	  			    		
	  			        }
	  			        ,yes: function(index, layero){
	  			          
	  			        //向服务端发送删除指令
	  				      $.ajax({
	  				    	  url:"/dbInfo/insert",
	  				    	  type:"PUT",
	  				    	  data:{
	  				    		  "dbName":$("#dbName").val(),
	  				    		  "dbType":$("#dbTypeResult").val(),
	  				    		  "dbAccessIp":$("#dbAccessIp").val(),
	  				    		  "port":$("#port").val(),
	  				    		  "passWord":$("#password").val()
	  				    	  },
	  				    	  dataType:"json",
	  				    	  success:function(result){
	  				    		  if(typeof(result) != "undefined" && result != null && result != ""){
	  				    			  if(result.code == 0){
	  				    				  layer.close(index);
	  				    				  reloadTable(false);
	  				    			      layer.msg(result.msg, {icon: 6}); 
	  				    			  }else{
	  				    				  layer.msg(result.msg, {icon: 5});
	  				    			  }
	  				    		  }
	  				    	  },error:function(){
	  				    		  layer.msg("保存员工信息请求失败", {icon: 5});
	  				    	  }
	  				      });
	  			        },
	  			        cancel:function(index, layero){
	  			        	layer.close(index);
	  			        }
	  			  });
        		break;
        	case 'update':
        		if(data.length === 0){
        			layer.msg('请选择一行');
        		} else if(data.length > 1){
        			layer.msg('只能同时编辑一个');
        		} else {
        			layer.open({
  	  				  title:"修改数据源信息"
  	  				  ,type: 1
  	  			        ,offset: 'auto' //具体配置参考：http://www.layui.com/doc/modules/layer.html#offset
  	  			        ,id: 'updateDataSourcePanel' //防止重复弹出
  	  			        ,content: $('#dataSoureInfoPanel')
  	  			        ,btn: ['确认', '取消']
        				,area:['500px','400px']
  	  			        ,btnAlign: 'c' //按钮居中
  	  			        ,closeBtn:2
  	  			        ,shade: 0.5 //不显示遮罩
  	  			        ,shadeClose:true
  	  			        ,anim:1
  	  			        ,resize:false
  	  			        ,success: function(layero, index){
  	  			        	$("#dbInfoId").val(data[0].dbInfoId);
  	  			        	$("#dbTypeResult").val(data[0].dbType);
  	  			    		form.val("dataSourceInfoFilter",{
  	  			    			"dbType":data[0].dbType,
  	  			    			"dbNameText":data[0].dbName,
  	  			    			"passText":data[0].passWord,
  	  			    			"dbAccessIpText":data[0].dbAccessIp,
  	  			    			"dbPortText":data[0].port
  	  			    		});
  	  			    		
  	  			        }
  	  			        ,yes: function(){
  	  			          layer.closeAll();
  	  			        //向服务端发送删除指令
  	  				      $.ajax({
  	  				    	  url:"/dbInfo/update",
  	  				    	  type:"POST",
  	  				    	  data:{
  	  				    		  "dbInfoId":$("#dbInfoId").val(),
  	  				    		  "dbName":$("#dbName").val(),
  	  				    		  "dbType":$("#dbTypeResult").val(),
  	  				    		  "dbAccessIp":$("#dbAccessIp").val(),
  	  				    		  "port":$("#port").val(),
  	  				    		  "passWord":$("#password").val()
  	  				    	  },
  	  				    	  dataType:"json",
  	  				    	  success:function(result){
  	  				    		  if(typeof(result) != "undefined" && result != null && result != ""){
  	  				    			  if(result.code == 0){
  	  				    				  reloadTable(false);
  	  				    			      layer.msg(result.msg, {icon: 6}); 
  	  				    			  }else{
  	  				    				  layer.msg(result.msg, {icon: 5});
  	  				    			  }
  	  				    		  }
  	  				    	  },error:function(){
  	  				    		  layer.msg("更新数据源请求失败", {icon: 5});
  	  				    	  }
  	  				      });
  	  			        },
  	  			        cancel:function(){
  	  			        	
  	  			        	layer.closeAll();
  	  			        }
  	  			  });
        		}
        		break;
        	case 'delete':
        		if(data.length === 0){
        			layer.msg('请选择一行');
        		} else {
        			$.ajax({
        				url:"/dbInfo/delete/"+data[0].dbInfoId,
        				method:"DELETE",
        				dataType:"json",
        				success:function(result){
        					if(typeof(result) != "undefined" && result != null && result != ""){
	  			    			  if(result.code == 0){
	  			    				  let isEmpty = false;
  				    				  if(tableDataLength == 1){
  				    					isEmpty = true;
  				    				  }
  				    				  reloadTable(isEmpty);
	  			    			      layer.msg(result.msg, {icon: 6}); 
	  			    			  }else{
	  			    				  layer.msg(result.msg, {icon: 5});
	  			    			  }
	  			    		}
        				},error:function(){
        					layer.msg('删除错误', {icon: 5});
        				}
        			});
        		}
        		break;
    	};
    });
});